package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.UploadBase64ReqDto;
import com.yixun.qfbao.apidto.UploadBase64ResDto;
import com.yixun.qfbao.config.FileConfig;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.vo.UploadBase64ReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;

/**
 * 上传附件
 * @author guifang.weng
 * @create 2017-11-23 17:19
 **/
@Service
public class UploadBase64Hander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(UploadBase64Hander.class);

    @Autowired
    private FileConfig fileConfig;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<UploadBase64ResDto> res = new BaseResponseDTO<>();
        UploadBase64ResDto resDto = new UploadBase64ResDto();
        UploadBase64ReqVo reqVo = (UploadBase64ReqVo)reqDto;
        UploadBase64ReqDto uploadBase64ReqDto = reqVo.getParams();
        MultipartHttpServletRequest mulRequest = uploadBase64ReqDto.getMulRequest();
        if (mulRequest == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("请选择附件上传");
            return res;
        }
        try {
            Iterator<String> fileNames = mulRequest.getFileNames();
            byte[] imgByte = null;
            // 遍历请求中的图片信息
            if (fileNames.hasNext()) {
                // 图片对应的参数名
                String fileName = fileNames.next();
                // 获取到图片
                MultipartFile file = mulRequest.getFile(fileName);
                if (file != null) {
                    // 可以获取到图片的字节数组
                    imgByte = file.getBytes();
                }
            }
            for (int i = 0; i < imgByte.length; ++i) {
                if (imgByte[i] < 0) {
                    //调整异常数据
                    imgByte[i] += 256;
                }
            }
            //上传图片至图片服务器
            String imgFilePath = fileConfig.getFileSet();
            //图片名称
            String imgName = "/" + String.valueOf(System.currentTimeMillis()) + ".png";
            OutputStream out = new FileOutputStream(imgFilePath + imgName);
            out.write(imgByte);
            out.flush();
            out.close();

            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setImgName(imgName);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("UploadBase64Hander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("上传附件失败");
        }
        return res;
    }
}
